package k4_thread.demo03;

public class SaleTicket extends Thread {

    //初始化一共有100张票,因为车票是所有火车站共享的，必须使用static进行修饰
    private static int totalTickets = 100;

    @Override
    public void run() {

        while(totalTickets>0){
            try {
                System.out.println(getName()+"卖了一张票，剩余"+ --totalTickets+"张票");
                Thread.sleep(200);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }
}

class Test{

    public static void main(String[] args) {

        System.out.println("开始卖票");

        Thread thread1 = new SaleTicket();
        Thread thread2 = new SaleTicket();
        Thread thread3 = new SaleTicket();

        thread1.setName("北京");
        thread2.setName("上海");
        thread3.setName("哈尔滨");

        thread1.start();
        thread2.start();
        thread3.start();

        try {
            thread1.join();
            thread2.join();
            thread3.join();
        }catch (Exception e){
            e.printStackTrace();
        }


        System.out.println("票已售罄");
    }

}
